package club.calong.sanniu.controller;
// 团   队: 风讯网络
// 后   端: Dopa
// 开发时间: 2021/7/23 14:25

import club.calong.sanniu.http.request.PlateRequest;
import club.calong.sanniu.http.response.Response;
import club.calong.sanniu.service.PlateService;
import club.calong.sanniu.util.CheckUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/plate")
public class PlateController {

    PlateService service;

    @Autowired
    public PlateController(PlateService service) {
        this.service = service;
    }

    @GetMapping("list/{page}/{size}")
    @PreAuthorize("hasAnyAuthority('content:select')")
    public Response list(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {

        return new Response(Response.SUCCESS, service.list(page, size));
    }

    @GetMapping("{id}")
    @PreAuthorize("hasAnyAuthority('content:select')")
    public Response one(@PathVariable("id") Integer id) {
        return new  Response(Response.SUCCESS, service.one(id));
    }

    @PostMapping
    @PreAuthorize("hasAnyAuthority('content:update')")
    public Response create(@RequestBody PlateRequest plateRequest) {

        plateRequest.setId(null);
        if (CheckUtils.checkPlate(plateRequest) && service.create(plateRequest)) {
            return new  Response(Response.SUCCESS, Response.Message.INSERT_OK, null);
        }
        return new  Response(Response.SUCCESS, Response.Message.INSERT_ERROR,null);
    }

    @PutMapping
    @PreAuthorize("hasAnyAuthority('content:update')")
    public Response update(@RequestBody PlateRequest plateRequest){

        if (CheckUtils.checkPlate(plateRequest) && service.update(plateRequest)) {
            return new  Response(Response.SUCCESS, Response.Message.UPDATE_OK, null);
        }
        return new  Response(Response.SUCCESS, Response.Message.INSERT_ERROR,null);
    }

    @DeleteMapping("{id}")
    @PreAuthorize("hasAnyAuthority('content:update')")
    public Response delete(@PathVariable("id") Integer id){

        if (service.delete(id)){
            return new  Response(Response.SUCCESS, Response.Message.DELETE_OK, null);
        }
        return new  Response(Response.FAILURE, Response.Message.DELETE_ERROR, null);
    }
}
